﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using MM.DBUtility;

namespace MobileManager
{
    public partial class MmarkArea : System.Web.UI.Page
    {
        private MM.Model.T_Users userModel;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurUser"] == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
            else
            {
                userModel = (MM.Model.T_Users)Session["CurUser"];
            }
            if (!IsPostBack)
            {

                BindDdlChannel(ref ddlChannel);
                ddlChannel.Items.Insert(0, new ListItem("全部", "-1"));

                BindDdlODEP(ref ddlODEP);
                ddlODEP.Items.Insert(0, new ListItem("全部", "-1"));

                BinDdlProvince(ref ddlProvince);
                ddlProvince.Items.Insert(0, new ListItem("全部", "-1"));

                AspNetPager1.CurrentPageIndex = 1;
                GVDataBind();
            }
        }
        #region DDL Bind
        private void BinDdlProvince(ref DropDownList ddl)
        {
            ddl.DataSource = BLSqlData.AllProvince;
            ddl.DataValueField = "ProvinceID";
            ddl.DataTextField = "ProvinceName";
            ddl.DataBind();
        }
        private void BindDdlODEP(ref DropDownList ddl)
        {
            ddl.DataSource = DbHelperSql.Query("SELECT ONO,ONO+'-'+ OName as Name FROM T_ODEP WITH(NOLOCK) ").Tables[0];
            ddl.DataValueField = "ONO";
            ddl.DataTextField = "Name";
            ddl.DataBind();
        }
        private void BindDdlChannel(ref DropDownList ddl)
        {
            ddl.DataSource = DbHelperSql.Query("SELECT ChannelNO,ChannelNO+'-'+ ChannelNumber+'-'+ChannelOrder as Name FROM T_Channel WITH(NOLOCK) order by ChannelNO").Tables[0];
            ddl.DataValueField = "ChannelNO";
            ddl.DataTextField = "Name";
            ddl.DataBind();
        }
        #endregion

        private void GVDataBind()
        {
            string strSQL = @"SELECT row_number() over(ORDER BY a.CreateTime DESC) rn,a.ChannelNO,a.CreateTime,b.ChannelNO+'-'+ b.ChannelNumber+'-'+b.ChannelOrder as ChannelName,a.ONO,c.OName,d.ProvinceName,e.CityName
                            FROM T_MaskArea a,T_Channel b,T_ODEP c,T_Province d,T_Area e WITH(NOLOCK) 
                            WHERE a.ChannelNO=b.ChannelNO AND a.ONO=c.ONO AND a.ProvinceID=d.ProvinceID AND a.AreaNO=e.AreaNO AND d.ProvinceID=e.ProvinceID ";

            if (!"-1".Equals(ddlChannel.SelectedValue))
                strSQL += string.Format(" AND a.ChannelNO='{0}'", ddlChannel.SelectedValue);
            if (!"-1".Equals(ddlODEP.SelectedValue))
                strSQL += string.Format(" AND a.ONO='{0}'", ddlODEP.SelectedValue);
            if (!"-1".Equals(ddlProvince.SelectedValue))
                strSQL += string.Format(" AND a.ProvinceID={0}", ddlProvince.SelectedValue);

            string strCityName = txtCity.Text.Trim();
            if (!string.IsNullOrEmpty(strCityName))
                strSQL += string.Format(" AND e.CityName like N'{0}%'", strCityName);

            AspNetPager1.RecordCount = DbHelperSql.GetCount("SELECT COUNT(1) FROM (" + strSQL + ") AS B");

            strSQL = string.Format("SELECT TOP {0} * FROM ({1}) AS F WHERE rn BETWEEN {2} AND {3}", AspNetPager1.PageSize, strSQL, (AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex * AspNetPager1.PageSize - 1);

            DataSet ds = DbHelperSql.Query(strSQL);
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            GVDataBind();
        }
        protected void ddlChannel_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindProvice(ddlChannel.SelectedValue);
        }
        private void BindProvice(string strNO)
        {
            ddlProvince.Items.Clear();
            if (!"-1".Equals(strNO))
            {
                DataTable dt = DbHelperSql.Query(string.Format("SELECT TOP 1 ChannelPro,ONO FROM T_Channel WITH(NOLOCK) WHERE ChannelNO='{0}'", strNO)).Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    string strPros = ";" + dt.Rows[0]["ChannelPro"].ToString();
                    ddlProvince.DataSource = BLSqlData.AllProvince.Where(e => strPros.Contains(";" + e.ProvinceID + ";")).ToList();
                    ddlProvince.DataValueField = "ProvinceID";
                    ddlProvince.DataTextField = "ProvinceName";
                    ddlProvince.DataBind();

                    ddlODEP.SelectedValue = dt.Rows[0]["ONO"].ToString();
                }
            }
            else
            {
                BinDdlProvince(ref ddlProvince);
                ddlProvince.Items.Add(new ListItem("全部", "-1"));
            }

        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            GVDataBind();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string strSQL = @"SELECT a.id FROM T_MaskArea a,T_Channel b,T_ODEP c,T_Province d,T_Area e WITH(NOLOCK) 
                            WHERE a.ChannelNO=b.ChannelNO AND a.ONO=c.ONO AND a.ProvinceID=d.ProvinceID AND a.AreaNO=e.AreaNO AND d.ProvinceID=e.ProvinceID ";

            if (!"-1".Equals(ddlChannel.SelectedValue))
                strSQL += string.Format(" AND a.ChannelNO='{0}'", ddlChannel.SelectedValue);
            if (!"-1".Equals(ddlODEP.SelectedValue))
                strSQL += string.Format(" AND a.ONO='{0}'", ddlODEP.SelectedValue);
            if (!"-1".Equals(ddlProvince.SelectedValue))
                strSQL += string.Format(" AND a.ProvinceID={0}", ddlProvince.SelectedValue);

            string strCityName = txtCity.Text.Trim();
            if (!string.IsNullOrEmpty(strCityName))
                strSQL += string.Format(" AND e.CityName like N'{0}%'", strCityName);

            DbHelperSql.ExecuteSql("DELETE FROM T_MaskArea WHERE ID IN (" + strSQL + ")");
            GVDataBind();
        }
    }
}
